package Servlet.member;

import Utils.DruidUtils;
import domain.SysUser;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 获取请求表单里面的数据
        String req_userName = request.getParameter("user");
        String req_pwd = request.getParameter("pwd");
        String req_code = request.getParameter("code");
        String req_remember = request.getParameter("remember");

        boolean flag = false;

        // 获取session
        HttpSession session = request.getSession();
        // 创建Cookie对象，将sessionID存入cookie中
        Cookie cookie = new Cookie("JSESSIONID", session.getId());

        // 判断输入是否合法
        if (req_userName == "" && req_pwd=="" && req_code=="") {
            // 将错误信息储存到域对象中
            request.setAttribute("login_from_info", "表单不能为空");
//            System.out.println("都为空");
            request.getRequestDispatcher(request.getContextPath()+"/login.jsp").forward(request,response);
            return;
        }else {
            request.setAttribute("login_from_info","");
        }

        // 将用户名和密码存入session
        session.setAttribute("login_req_user", req_userName);
        session.setAttribute("login_req_pwd", req_pwd);
        session.setAttribute("login_req_code", req_code);
//        System.out.println("登录页面的code：" + session.getAttribute("code"));


        // 在数据库中取值
        try {

            String sql="select user_name as userName, pwd, tel from test";

            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
            List<SysUser> userList = queryRunner.query(sql, new BeanListHandler<>(SysUser.class));
            for (SysUser user : userList) {
                if (req_userName.equals(user.getUserName()) && req_pwd.equals(user.getPwd())){
                    flag = true;
                    session.setAttribute("User", user);
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }

        // flag为true时进行数据保存处理；
        if (flag) {
            request.setAttribute("login_user_info","");
            // 判断验证码是否正确
            if (req_code != null && req_code.equals(session.getAttribute("code"))) {
                request.setAttribute("login_code_info","");
//                System.out.println("User对象："+User);

                // 判断用户是否记住用户名和密码
                if (req_remember != null && req_remember.equals("1")) {
                    // 设置cookie失效时间
                    cookie.setMaxAge(60 * 100000);
                    // 给响应添加cookie
                    response.addCookie(cookie);
                } else {
                    // 给响应添加cookie
                    response.addCookie(cookie);
                }

                // 跳转到/index.jsp页面
//                request.getRequestDispatcher("/news").forward(request,response);
                response.sendRedirect("/javaweb/news");
            } else {
//                System.out.println("验证码不正确");
                // 将错误信息储存到域对象中
                request.setAttribute("login_code_info","验证码不正确");
                // 跳转到login.jsp页面，用户重新输入
                request.getRequestDispatcher(request.getContextPath()+"/login.jsp").forward(request,response);
            }
        }else {
            // 将错误信息储存到域对象中
            request.setAttribute("login_user_info","用户不存在或密码错误");
            // 跳转到login.jsp页面，用户重新输入
            request.getRequestDispatcher(request.getContextPath()+"/login.jsp").forward(request,response);
        }
    }
}
